#include "../common.h"
using namespace std;
extern BuiltinCmd blt[MAX_BUILTIN_FUNC_NUM];
extern obj_index _index;
extern obj_index indexs[MAX_REFS_HEADS_NUMS];
extern char buf[80];
extern char _obj[MAX_BUILTIN_DESC_LENGTH];
extern char _GIT[MAX_BUILTIN_DESC_LENGTH];
extern char txt_index[MAX_BUILTIN_DESC_LENGTH];
extern char txt_HEAD[MAX_BUILTIN_DESC_LENGTH];
extern char _refs[MAX_BUILTIN_DESC_LENGTH];
extern char refs_heads[MAX_BUILTIN_DESC_LENGTH];
extern char _log[MAX_BUILTIN_DESC_LENGTH];
extern char log_logs[MAX_BUILTIN_DESC_LENGTH];
extern REFS_heads _heads[MAX_REFS_HEADS_NUMS];
extern char SHA_1[41];
extern char obj_sha[1024];
obj_index INDEX;

int git_commit_cmdpline(int pos,int retc,char **retv){
	return 0;
}



int GET_SHA(char *rretv){
    char retv[1024];strcpy(retv,rretv);
	getcwd(buf, sizeof(buf));
	char ch[1024];strcpy(ch,buf);
	strcat(ch,"\\.git\\"); strcat(ch,retv);
	strcpy(retv,ch);
	FILE *fp=fopen("D:\\self-git\\git\\sha1\\sha.txt","w");
	fwrite(retv,1,strlen(retv),fp);
	fclose(fp);
	system("D:\\self-git\\git\\sha1\\sha1.exe");
	fp=fopen("D:\\self-git\\git\\sha1\\sha_write.txt","r");
	  fgets(SHA_1,41,fp);
	fclose(fp); 
}

int git_commit_run(int retc,char **retv){
	_init();
	char refs_path[MAX_BUILTIN_DESC_LENGTH]="";
	FILE *fp=fopen(txt_HEAD,"r");
	fgets(refs_path,MAX_BUILTIN_DESC_LENGTH,fp);
	fclose(fp);
	if (strcmp(refs_path,"")==0){
		GET_SHA("index.dat");
		get_sha_path(SHA_1);
		strcpy(_index.author,"zpykillcc");
		strcpy(_index.parent,obj_sha);
		strcpy(_index.tree,obj_sha);
		strcpy(_index._tips,retv[4]);
		strcpy(_index.file_type,"tree");
		FILE *fp=fopen(obj_sha,"w"); 
		   fwrite(&_index,sizeof(obj_index),1,fp);
		   FILE *fp1=fopen(txt_index,"r");
		     int files;obj_index _INDEX;
			 while(fread(&_INDEX,sizeof(obj_index),1,fp1)){
		       ++files;
		       fwrite(&_INDEX,sizeof(obj_index),1,fp);
		       printf("%s have been added\n",_INDEX.file_path);
		     }
		   fclose(fp1);
		fclose(fp);
		fp=fopen(txt_HEAD,"w");
		fwrite(_heads[1].name,strlen(_heads[1].name),1,fp);
		fclose(fp);
		fp=fopen(_heads[1].name,"w");
		fwrite(obj_sha,strlen(obj_sha),1,fp);
		fclose(fp);
		printf("%d files changed",files);
		
		
	    strcpy(INDEX.author,"zpykillcc");
	    strcpy(INDEX.tree,SHA_1);
	    strcpy(INDEX.file_sha_path,obj_sha);
	    strcpy(INDEX._tips,retv[4]);
	    fp=fopen(log_logs,"w");
	      fwrite(&INDEX,sizeof(obj_index),1,fp);
	    fclose(fp);
		return 0;
	}
	fp=fopen(refs_path,"r");
	   fgets(refs_path,MAX_BUILTIN_DESC_LENGTH,fp);
	fclose(fp);//打开refs heads中文件
	
		int files=0;
		//寻找add与modify 
		FILE *fp1=fopen(txt_index,"r");
		while(fread(&_index,sizeof(obj_index),1,fp1)){
			bool flag_add=false;
			bool flag_modify=false; 
			fp=fopen(refs_path,"r");//打开当前分支obj中文件
	        obj_index _INDEX;
	        fread(&_INDEX,sizeof(obj_index),1,fp);
			while (fread(&_INDEX,sizeof(obj_index),1,fp)){
				if (strcmp(_INDEX.file_path,_index.file_path)==0 && strcmp(_INDEX.tree,_index.tree)==0)flag_add=true; 
				if (strcmp(_INDEX.file_path,_index.file_path)==0)flag_modify=true; 
			}
			if (flag_modify==false){
				printf("%s have been added\n",_index.file_path);
				files++;
			}
			else if (flag_modify==true && flag_add==false){
				printf("%s have been modified\n",_index.file_path);
				files++;
			}
			fclose(fp);
		}
	    fclose(fp1);
	    
	    //寻找rm的文件 
		fp=fopen(refs_path,"r");
		fread(&_index,sizeof(obj_index),1,fp);
		while(fread(&_index,sizeof(obj_index),1,fp)){
			bool flag=false; 
			fp1=fopen(txt_index,"r");//打开当前分支index缓存中文件
	        obj_index _INDEX;
			while (fread(&_INDEX,sizeof(obj_index),1,fp1)){
				if (strcmp(_INDEX.file_path,_index.file_path)==0)flag=true;
			}
			if (flag==false){
				printf("%s have been deleted\n",_index.file_path);
				files++;
			}
			fclose(fp1);
		}
	    fclose(fp);

	if (!files){
		printf("On branch master\n");
		printf("nothing to commit, working tree clean\n");
		return 0;
	}
	else{
		printf("%d files changed\n",files);
	}	
	GET_SHA("index.dat");
	get_sha_path(SHA_1);
	fp=fopen(refs_path,"r");//打开当前分支obj中文件
	obj_index _INDEX;
	fread(&_INDEX,sizeof(obj_index),1,fp);
	strcpy(_INDEX.parent,_INDEX.tree);
	strcpy(_INDEX.tree,obj_sha);
	strcpy(_INDEX._tips,retv[4]);
	fclose(fp);
    // 将当前index 进行commit进库 
	fp=fopen(obj_sha,"w");
	    fwrite(&_INDEX,sizeof(_INDEX),1,fp);
		fp1=fopen(txt_index,"r");
		while(fread(&_index,sizeof(_index),1,fp1)){
		       fwrite(&_index,1,sizeof(_index),fp);
		     }
		   fclose(fp1);
	fclose(fp);
	
	fp=fopen(txt_HEAD,"r"); // 将当前路径写入refs中 
	fgets(refs_path,MAX_BUILTIN_DESC_LENGTH,fp);
	fclose(fp);
	fp=fopen(refs_path,"w");
	fwrite(obj_sha,strlen(obj_sha),1,fp);
	fclose(fp);
	
	// 将本次commit存入log中
	strcpy(INDEX.author,"zpykillcc");
	strcpy(INDEX.tree,SHA_1);
	strcpy(INDEX.file_sha_path,obj_sha);
	strcpy(INDEX._tips,retv[4]);
	int logs_num=0;
	fp=fopen(log_logs,"r"); 
	while(fread(&_index,sizeof(obj_index),1,fp))indexs[++logs_num]=_index;
	
	fclose(fp);
	fp=fopen(log_logs,"w");
	   fwrite(&INDEX,sizeof(obj_index),1,fp);
	   for (int i=1;i<=logs_num;i++)fwrite(&indexs[i],sizeof(obj_index),1,fp);
	fclose(fp);
}
int git_commit() {
    BuiltinCmd x;
    strcpy(x.name, "commit");
    strcpy(x.desc, "commit the staging area content to the warehouse");
    x.hasAction = false;
    x.optNum = 2;
    strcpy(x.Help,"git commit");
	strcpy(x.action, "Author identity unknown");
	addBuiltinCmd(&x);
    return 0;
}

